package com.qidian.demo.controller;

import com.qidian.demo.pojo.Item;
import com.qidian.demo.service.ItemService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.annotation.Resource;
/**
 * @author 戴着假发的程序员
 */
@RestController
@RequestMapping("/item")
public class ItemController {
    @Resource
    private ItemService itemService;
    /**
     * @param item 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public Item selectOne(Item item) {
        Item result = itemService.selectById(item.getItemId());
        if(result != null){
          return  result;
        }
        return null;
    }
    /**
     * 新增一条数据
     * @param item 实体类
     * @return Map
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public Map insert(Item item) {
        Map map = new HashMap();
        map.put("code",200);
        int result = itemService.insert(item);
        if (result <= 0) {
          map.put("code",500);
        }
        return map;
    }
    /**
     * 修改一条数据
     * @param item 实体类
     * @return Map
     */
    @RequestMapping(value = "update")
    public Map update(Item item) {
        Map map = new HashMap();
        map.put("code",200);
        Item result = itemService.update(item);
        if (result != null) {
            map.put("data",result);
        }else{
            map.put("code",500);
        }
        return map;
    }
    /**
     * 删除一条数据
     * @param item 参数对象
     * @return Map
     */
    @RequestMapping(value = "delete")
    public Map delete(Item item) {
        Map map = new HashMap();
        map.put("code",200);
        int result = itemService.deleteById(item.getItemId());
        if (result <= 0) {
           map.put("code",500);
        }
        return map;
    }
    /**
     * 查询全部
     * @return Response对象
     */
    @RequestMapping(value = "selectAll")
    public List<Item> selectAll() {
        return itemService.selectAll();
    }
}